package cn.cyl.basic.controller;

import cn.cyl.basic.dto.PhoneCodeDto;
import cn.cyl.basic.exception.BusinessException;
import cn.cyl.basic.service.VerifyCodeService;
import cn.cyl.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {

    @Autowired
    private VerifyCodeService verifyCodeService;

    @GetMapping("/image/{key}")
    public AjaxResult getImageCode(@PathVariable("key") String key){
        try {
            verifyCodeService.getImageCode(key);
            AjaxResult ajaxResult = new AjaxResult();
            ajaxResult.setObj(verifyCodeService.getImageCode(key));
            return ajaxResult;
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"网络异常");
        }
    }

    @PostMapping("/smsCode")
    public AjaxResult getPhoneCode(@RequestBody PhoneCodeDto dto){
        try {
            verifyCodeService.phoneVerifyCode(dto);
            return new AjaxResult();
        } catch (BusinessException e) {
            return new AjaxResult(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"网络异常");
        }
    }

    @PostMapping("/binderSmsCode")
    public AjaxResult binderSmsCode(@RequestBody PhoneCodeDto dto){
        try {
            verifyCodeService.binderSmsCode(dto);
            return new AjaxResult();
        } catch (BusinessException e) {
            return new AjaxResult(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"网络异常");
        }
    }


}
